package com.codeduck.poi.controller.excel;

import com.codeduck.poi.util.AttachmentExportUtil;
import com.google.common.base.Strings;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.DataNode;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: <a href="mailto:codeduck@163.com">Codeduck</a>
 * @date: 2023/2/5
 */
@RestController
public class WebTableDataWriteIntoExcelController {

    @GetMapping("/read/from/web/table")
    public void readDataFromWikipedia(HttpServletResponse response) {
        Workbook workbook = new XSSFWorkbook();
        // 单元格样式创建
        CellStyle cellStyle = workbook.createCellStyle();
        //垂直居中
        cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
        // 水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
        Sheet sheet = workbook.createSheet("sheet1");

        Document document = null;
        try {
            document = Jsoup.connect("https://www.runoob.com/tags/tag-table.html").get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        Element table = document.select("table").last();
        Element tbody = table.select("tbody").first();

        // 初始化表头
        Element firstEl = tbody.select("tr").first();
        Elements headRow = firstEl.select("th");
        for (int i = 0; i < headRow.size(); i++) {
            setCellData(sheet, cellStyle, 0, i, headRow.get(i).text());
        }

        // 填充表体数据
        Elements trs = tbody.select("tr");
        for (int r = 1; r < trs.size(); r++) {
            Elements tds = trs.get(r).select("td");
            for (int c = 0; c < tds.size(); c++) {
                setCellData(sheet, cellStyle, r, c, tds.get(c).text());
            }
        }

        AttachmentExportUtil.export(workbook, "readDataFromWebTable", response);
    }



    /**
     * 单元格赋值并设置样式
     */
    private void setCellData(Sheet sheet, CellStyle cellStyle, int firstRow, int firstCol, String val) {
        if (sheet == null) return;

        // 初始化cell
        if (sheet.getRow(firstRow) == null) {
            sheet.createRow(firstRow);
        }
        Row row = sheet.getRow(firstRow);
        Cell cell = row.createCell(firstCol);
        // 设置样式
        cell.setCellStyle(cellStyle);
        // 设置value
        cell.setCellValue(val);
        // 根据标题设置单元格宽度
        sheet.setColumnWidth(firstCol, cell.getStringCellValue().length() * 256 * 2);
    }
}
